package com.linkage.educloud.ah.bean;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * <desc>
 * Created by The Moss on 2018/6/11.
 */

public class AdsBean extends BaseApiRet implements Parcelable {

    // 类型 0:分类 1:单品
    private int type;
    // 来源 0:淘宝 9:京东
    private int platform;
    // 分类ID
    private String catId;
    // 分类名称
    private String catName;
    // 展示图片
    private String imgUrl;

    public static List<AdsBean> fromJSONObject(JSONObject json){
        List<AdsBean> adsBeans = new ArrayList<>();
        try {
            if (json.has("ads") && !json.isNull("ads")) {
                JSONArray jar = json.getJSONArray("ads");
                for (int i = 0; i < jar.length(); i++) {
                    AdsBean bean = new AdsBean();
                    JSONObject obj = jar.getJSONObject(i);
                    bean.catId = obj.optString("itemId");
                    bean.type = obj.optInt("type");
                    bean.platform = obj.optInt("platform");
                    bean.catName = obj.optString("itemName");
                    bean.imgUrl = obj.optString("imageUrl");
                    adsBeans.add(bean);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return adsBeans;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getPlatform() {
        return platform;
    }

    public void setPlatform(int platform) {
        this.platform = platform;
    }

    public String getCatId() {
        return catId;
    }

    public void setCatId(String catId) {
        this.catId = catId;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.type);
        dest.writeInt(this.platform);
        dest.writeString(this.catId);
        dest.writeString(this.catName);
        dest.writeString(this.imgUrl);
    }

    public AdsBean() {
    }

    protected AdsBean(Parcel in) {
        this.type = in.readInt();
        this.platform = in.readInt();
        this.catId = in.readString();
        this.catName = in.readString();
        this.imgUrl = in.readString();
    }

    public static final Parcelable.Creator<AdsBean> CREATOR = new Parcelable.Creator<AdsBean>() {
        @Override
        public AdsBean createFromParcel(Parcel source) {
            return new AdsBean(source);
        }

        @Override
        public AdsBean[] newArray(int size) {
            return new AdsBean[size];
        }
    };
}
